package tallinn;

import java.util.Hashtable;
import java.util.Vector;

import processing.core.PApplet;
import simpleGui.PCamera3D;

public class Tallin2 extends PApplet {
	String path="/Users/administrator/Desktop/tallinn/";
	String pathElements="/Users/administrator/Desktop/tallinn/partsFacade.csv";
	Vector<Element>facades;
	Hashtable<String,Type>types;
	PCamera3D pcam3D;
	float[]heights=new float[7];
public void setup(){
	size(800,600,P3D);
	pcam3D = new PCamera3D(this);
	
	for (int i=1;i<heights.length;i++){
		heights[i]=(i-1)*2740+1150;
		
	}
	println(heights);
	loadParts(pathElements);
	facades=new Vector<Element>();
	Element[][] f1=loadFacade(path+"f1.csv",1,0,0,0);
	Element[][] f2=loadFacade(path+"f2.csv",2,f1[f1.length-1][0].x2,0,0);
	Element[][] f3=loadFacade(path+"f3.csv",3,f2[f2.length-1][0].x2,f2[f2.length-1][0].y2,0);
	Element[][] f4=loadFacade(path+"f4.csv",4,f3[f3.length-1][0].x2,f3[f3.length-1][0].y2,0);
}
public void loadParts(String pathElements){
	types=new Hashtable<String,Type>();
	String[] stringElements=this.loadStrings(pathElements);
	for (int i=1;i<stringElements.length;i++){
		String[] data=stringElements[i].split(",");
		Type type=new Type(data[0],Float.valueOf(data[1]),Float.valueOf(data[2]));
		types.put(type.label, type);
		println(type.label);
	}
}
public Element[][] loadFacade(String path,int orientation,float sX,float sY,float sZ){
	String[] stringElements=this.loadStrings(path);
	int nZ=stringElements.length;
	int nX=stringElements[0].split(",").length;
	Element[][] eArray=new Element[nX][nZ];
	float cX=sX,cY=sY,cZ=sZ;
	for (int ii=0;ii<stringElements.length;ii++){
		String[] data=stringElements[ii].split(",");
		if (orientation==1)cX=sX;
		if (orientation==2)cY=sY;
		if (orientation==3)cX=sX;
		if (orientation==4)cY=sY;
		int i=6-ii-1;
		float z=heights[i+1];
		for (int j=0;j<data.length;j++){
			
			Type type=types.get("NS "+data[j]);
			println(data[j]);
			eArray[j][i] =new Element();
			eArray[j][i].type=type;
			eArray[j][i].x1=cX;
			eArray[j][i].y1=cY;
			eArray[j][i].z1=z-type.height;
			if (orientation==1)cX+=type.length;
			if (orientation==2)cY+=type.length;
			if (orientation==3)cX-=type.length;	
			if (orientation==4)cY-=type.length;
			eArray[j][i].x2=cX;
			eArray[j][i].y2=cY;
			eArray[j][i].z2=z;
			facades.add(eArray[j][i]);
		}
		cZ+=types.get("NS "+data[data.length-1]).height;
	}
	return eArray;
}
public void draw(){
	background(255);
	pcam3D.apply(g);
	scale(0.00005f);
	for (int i=0;i<facades.size();i++){
		Element el=facades.get(i);
		beginShape(QUADS);
		vertex(el.x1,el.y1,el.z1);
		vertex(el.x2,el.y2,el.z1);
		vertex(el.x2,el.y2,el.z2);
		vertex(el.x1,el.y1,el.z2);
		endShape();
	}
}
class Element{
	float x1,y1,z1;
	float x2,y2,z2;
	Type type;
}
class Type{
	String label;
	float length,height,thickness;
	public Type(String label,float length,float height){
		this.label=label;
		this.length=length;
		this.height=height;
	}
	
}
}
